/**
 * @file drv_gpio.h
 * @author your name (you@domain.com)
 * @brief 
 * @version 0.1
 * @date 2024-09-02
 * 
 * @copyright Copyright (c) 2024
 * 
 */
#ifndef __DRV_GPIO_H__
#define __DRV_GPIO_H__

#include "n32g031.h"
#include <rtdef.h>

#if defined(GPIOZ)
#define _N32_PORT_MAX 12u
#elif defined(GPIOK)
#define _N32_PORT_MAX 11u
#elif defined(GPIOJ)
#define _N32_PORT_MAX 10u
#elif defined(GPIOI)
#define _N32_PORT_MAX 9u
#elif defined(GPIOH)
#define _N32_PORT_MAX 8u
#elif defined(GPIOG)
#define _N32_PORT_MAX 7u
#elif defined(GPIOF)
#define _N32_PORT_MAX 6u
#elif defined(GPIOE)
#define _N32_PORT_MAX 5u
#elif defined(GPIOD)
#define _N32_PORT_MAX 4u
#elif defined(GPIOC)
#define _N32_PORT_MAX 3u
#elif defined(GPIOB)
#define _N32_PORT_MAX 2u
#elif defined(GPIOA)
#define _N32_PORT_MAX 1u
#else
#define _N32_PORT_MAX 0u
#error Unsupported N32 GPIO peripheral.
#endif

#define PIN_STPORT_MAX _N32_PORT_MAX

#define __N32_PORT(port)  GPIO##port##_BASE

#define GET_PIN(PORTx,PIN) (rt_base_t)((16 * ( ((rt_base_t)__N32_PORT(PORTx) - (rt_base_t)GPIOA_BASE)/(0x0400UL) )) + PIN)

#define PIN_NUM(port, no) (((((port) & 0xFu) << 4) | ((no) & 0xFu)))
#define PIN_PORT(pin) ((uint8_t)(((pin) >> 4) & 0xFu))
#define PIN_NO(pin) ((uint8_t)((pin) & 0xFu))

#define PIN_STPORT(pin) ((GPIO_Module *)(GPIOA_BASE + (0x400u * PIN_PORT(pin))))

#define PIN_STPIN(pin) ((uint16_t)(1u << PIN_NO(pin)))

struct pin_irq_map
{
    rt_uint16_t pinbit;
    IRQn_Type irqno;
};

#endif
